<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Cart;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\User;
use Hash;
class LoginController extends Controller
{
    //加载登录页面
    public function getIndex()
    {
        return view('home.login');
    }

    //登录
    public function postDologin(request $request)
    {
        $unum = $request->input('unum');
        $upassword = $request->input('upassword');
        $data = User::where('uemail',$unum)->orWhere('uphone',$unum)->orWhere('uname',$unum)->first();
        if($data){
            if($data->ustatus){
                if (Hash::check($upassword, $data->upassword)){
                    session(['user'=>$data]);
                    return redirect('/home/index')->with('success','登录成功');
                }else{
                    return back()->with('error','用户名或密码错误')->withinput();
                }
            }else{
                 return back()->with('error','您的账号已被禁用')->withinput();
            }
        }else{
            return back()->with('error','用户名或密码错误')->withinput();
        }
    }

    //注销
    public function getDologout()
    {


        session()->pull('user');
        Cart::destroy();


        return redirect('/home/login')->with('success','注销成功');
    }

}
